<?php

if (!defined('FosfoaSystem'))
{
    exit(1);
}
class categoriesModel extends spModel
{
    public $pk = 'categories_id';
    public $table = 'categories';
    public $dir;
    // 由spModel的变量$linker来设置表间关联
    var $linker = array(array('type' => 'hasone', // 关联类型，这里是一对一关联
        'map' => 'detail', // 关联的标识
        'mapkey' => 'categories_id', // 本表与对应表关联的字段名
        'fclass' => 'categories_descriptionModel', // 对应表的类名
        'fkey' => 'categories_id', // 对应表中关联的字段名
        'enabled' => true // 启用关联
        ));
    
    public function dir($parent_id){
        
        $aaar=$this->Detail($parent_id);
        if(!$aaar){
            return false;
        }
        $i=0;
        foreach ($aaar as $k=>$v)
        {
        $arr[$i]['categories_id']=$v['categories_id'];
        $arr[$i]['categories_name']=$v['categories_name'];
        $arr[$i]['alias']=$v['alias'];
        $arr[$i]['image']=$v['image'];
        $arr[$i]['parent_id']=$k;
        $i=$i+1;
        }
        return $arr;
    }
    
    public function Detail($parent_id){
		$condition = array('categories_id'=>$parent_id);
        $fields="categories_id,parent_id,alias,image";
		$arr = $this->spLinker()->find($condition,'sort_order',$fields);
        if(!$arr){
            return $this->dir=false;
        }
        if($arr['parent_id']==0){
            $this->dir[$arr['parent_id']]['categories_id']=$arr['categories_id'];
            $this->dir[$arr['parent_id']]['categories_name']=$arr['detail']['categories_name'];
            $this->dir[$arr['parent_id']]['alias']=$arr['alias'];
            $this->dir[$arr['parent_id']]['image']=$arr['image'];
         //dump($dir2);
        }else{
            $this->dir[$arr['parent_id']]['categories_id']=$arr['categories_id'];
            $this->dir[$arr['parent_id']]['categories_name']=$arr['detail']['categories_name'];
            $this->dir[$arr['parent_id']]['alias']=$arr['alias'];
            $this->dir[$arr['parent_id']]['image']=$arr['image'];
            $parent_id2=$arr['parent_id'];
            $this->Detail($parent_id2);
        }
        
        return $this->dir;	
	}
    

    /**
     * 获取单个分类的信息
     */
    function CDetail2($Cid){
        $Carray=array();
		$condition = array('categories_id'=>$Cid);
        $fields="categories_id,parent_id";
		$Carray = $this->spLinker()->find($condition,null,$fields);
        dump($Carray);
        if(!$Carray){
            return false;
        }
        
        return $Carray;	
	}
    /**
     * 获取父父父级分类名
     */
    function breadcrumb($cid)
    {
        $categories_dir_arr = array();
        $categories_dir_arr2 = array();
        $categories_dir_arr = $this->dir($cid);
        if ($categories_dir_arr) {
            //dump($categories_dir_arr);
            $ii = 0;
            for ($i = count($categories_dir_arr) - 1; $i >= 0; $i--) {
                $categories_dir_arr2[$ii]['categories_id'] = $categories_dir_arr[$i]['categories_id'];
                $categories_dir_arr2[$ii]['categories_name'] = $categories_dir_arr[$i]['categories_name'];
                $categories_dir_arr2[$ii]['alias'] = $categories_dir_arr[$i]['alias'];
                $categories_dir_arr2[$ii]['image'] = $categories_dir_arr[$i]['image'];
                $categories_dir_arr2[$ii]['parent_id'] = $categories_dir_arr[$i]['parent_id'];
                $ii = $ii + 1;
            }
        }
        //$categories_dir_arr2[$ii]['cid']= $cid;
        //$categories_dir_arr2[$ii]['cname']= $cname;
        return $categories_dir_arr2;
    }
    
    /**
     * 返回指定ID下的子类
     */
    function Categories($cid=0)
    {
        if($cid==0){
            $conditions='1=1';
        }else{
            $conditions=array('parent_id'=>$cid);
        }
        $this->linker[0]['field'] = 'categories_name,categories_title';
        $orders='sort_order asc,categories_id asc';
        $fields='categories_id,parent_id,alias,image';
       return $this->spLinker()->findAll($conditions, $orders, $fields); //产品分类
    }
    
    /**
     * 返回指定ID下的子子子类
     */
    function Categories_son($cid=0)
    {
        $Carry = $this->spCache(3600 * 24)->Categories(); //获取产品中心下的所有分类
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($Carry, $fields);
        $categories = $tree->leaf($cid);
        return $categories;
    }
    
    
    /**
     * 别名检测
     */
    function aliascheck($alias,$categories_id)
    {
        $conditions=array('alias'=>$alias);
        $request=$this->find($conditions);
        if($request){
            if($request['categories_id']==$categories_id)
            {
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }      
    }
    
    /**
     * 根据别名获取信息
     */
    function FindformAlias($alias)
    {
        $conditions=array('alias'=>$alias);
        $request=$this->spLinker()->find($conditions);
        if($request){
                return $request;
        }else{
            return false;
        } 
    }
    
    /**
     * 根据分类ID获取信息
     */
    function FindformID($categories_id)
    {
        $conditions=array('categories_id'=>$categories_id);
        $request=$this->spLinker()->find($conditions);
        if($request){
                return $request;
        }else{
            return false;
        } 
    }
}